package com.rent.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rent.common.exception.BusinessException;
import com.rent.common.interceptor.UserContext;
import com.rent.house.dao.CollectionDao;
import com.rent.house.dao.HouseResourceDao;
import com.rent.house.pojo.entity.Collection;
import com.rent.house.pojo.entity.HouseResource;
import com.rent.house.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收藏服务实现类
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionDao, Collection> implements CollectionService {

    @Autowired
    private HouseResourceDao houseResourceDao;

    @Override
    public boolean collectHouse(Long houseId) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new BusinessException("请先登录");
        }

        // 检查是否已收藏
        if (isCollected(houseId)) {
            return true;
        }

        Collection collection = new Collection();
        collection.setUserId(userId);
        collection.setHouseId(houseId);
        return this.save(collection);
    }

    @Override
    public boolean uncollectHouse(Long houseId) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new BusinessException("请先登录");
        }

        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("house_id", houseId);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean isCollected(Long houseId) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return false;
        }

        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("house_id", houseId);
        return this.count(queryWrapper) > 0;
    }

    @Override
    public Page<Collection> getMyCollections(Integer pageNo, Integer pageSize) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new BusinessException("请先登录");
        }

        Page<Collection> mpPage = new Page<>(pageNo, pageSize);

        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time");

        return this.page(mpPage, queryWrapper);
    }

    @Override
    public Page<HouseResource> getMyCollectionHouses(Integer pageNo, Integer pageSize) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new BusinessException("请先登录");
        }

        // 先查询用户收藏的房源ID列表
        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("user_id", userId);
        List<Collection> collections = this.list(collectionQueryWrapper);

        if (collections.isEmpty()) {
            // 如果没有收藏，返回空分页
            return new Page<>(pageNo, pageSize, 0);
        }

        // 提取房源ID列表
        List<Long> houseIds = collections.stream()
                .map(Collection::getHouseId)
                .collect(Collectors.toList());

        // 查询房源信息
        Page<HouseResource> page = new Page<>(pageNo, pageSize);
        QueryWrapper<HouseResource> houseQueryWrapper = new QueryWrapper<>();
        houseQueryWrapper.in("id", houseIds);
        houseQueryWrapper.eq("audit_status", 1);
        houseQueryWrapper.eq("status", 1);
        houseQueryWrapper.orderByDesc("create_time");

        return houseResourceDao.selectPage(page, houseQueryWrapper);
    }
}
